<?php

namespace app\index\controller;

use app\common\controller\Frontend;
use fast\Random;
use think\Config;
use think\Cookie;
use think\Hook;
use think\Request;
use think\captcha\Captcha;
use think\Db;
use think\Session;
use think\Validate;

/**
 * 会员中心
 */
class Register extends Frontend
{

    protected $layout = false;
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    public function _initialize()
    {
//        $this->view->engine->layout(false);
        parent::_initialize();
    }

    /**
     * 注册模式2
    **/
    public function reg2(Request $request){
        $ip = $request->ip();
        $this->assign('ip',$ip);
        if ($request->isPost()){
            $User = input('info/a',[]);
//            $res2 = $this->validate($User,'User.regist2');

//            if ($res2 !== true){
//                $this->success($res2);exit;
//            };

            $user = model('apply');

            $ret = $user->getByTel($User['tel']);

            // 如果已经注册过了
            if ( $ret ){

                $r = [
                    'update_time'=>time(),
                    'ip' => $request->ip(),
                    'is_reg'=>Db::raw('is_reg+1')
                ];

                $r = array_merge($User,$r);

                $res3 = $user->allowField(true)->isUpdate(true)->save($r,['id'=>$ret->id]);

                if ( $res3 ){
                    $user->bet($User['tel']);
                    $this->redirect('index/home');
                    exit;
                }else{
                    $this->error($res3);
                }
            }else{
                $User['is_reg'] = 0;
                $User['update_time'] = time();
                $User['ip'] = $request->ip();
                $User['password'] = md5($User['password']);

                if ($res3 = $user->allowField(true)->save($User)){
                    $user->bet($User['tel']);
                    $this->redirect('index/home');
                    exit;
                }else{
                    $this->error($res3);
                }
            }
        }
        return $this->view->fetch();
    }
    /**
     * 注册模式3
    **/
  /*  public function reg3(Request $request){
        if ($request->isPost()){
            $User = input('info/a',[]);
            $User['__token__'] = $token = input('__token__');

//            $res2 = $this->validate($User,'User.regist3');

//            if ($res2 !== true){
//                $this->success($res2);exit;
//            };


            $user = model('apply');

            $ret = $user->getByTel($User['tel']);


            // 如果已经注册过了
            if ( $ret ){
                $r = [
                    'update_time'=>time(),
                    'ip' => $request->ip(),
                    'is_reg'=>Db::raw('is_reg+1')
                ];

                $r = array_merge($User,$r);

                $res3 = $user->allowField(true)->isUpdate(true)->save($r,['id'=>$ret->id]);

                if ( $res3 ){
                    $user->bet($User['tel']);
                    $this->redirect('index/achieve');
                    exit;
                }else{
                    $this->error($res3);
                }
            }else{
                $User['is_reg'] = 0;
                $User['update_time'] = time();
                $User['ip'] = $request->ip();

                if ($res3 = $user->allowField(true)->save($User)){
                    $user->bet($User['tel']);
                    $this->redirect('index/achieve');
                    exit;
                }else{
                    $this->error($res3);
                }
            }
        }

        return $this->view->fetch();
    }*/

    public function yzm(){
        $config =    [
            // 验证码字体大小
            'fontSize'    =>    18,
            // 验证码位数
            'length'      =>    4,
            // 关闭验证码杂点
            'useNoise'    =>    false,
            'fontttf' => '6.ttf',
            'codeSet'=>'1234567890'
        ];

        $captcha = new Captcha($config);
        return $captcha->entry('10');
    }




}
